﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Test
{
    class MyTestTextBox  :UserControl
    {
        public TextBox textBox1;

        private void InitializeComponent()
        {
            this.textBox1 = new System.Windows.Forms.TextBox();
            this.SuspendLayout();
            // 
            // textBox1
            // 
            this.textBox1.AccessibleRole = System.Windows.Forms.AccessibleRole.TitleBar;
            this.textBox1.BackColor = System.Drawing.Color.Red;
            this.textBox1.Location = new System.Drawing.Point(130, 16);
            this.textBox1.Name = "textBox1";
            this.textBox1.Size = new System.Drawing.Size(120, 25);
            this.textBox1.TabIndex = 0;
            // 
            // MyTestTextBox
            // 
            this.BackColor = System.Drawing.SystemColors.ControlText;
            this.Controls.Add(this.textBox1);
            this.Name = "MyTestTextBox";
            this.Size = new System.Drawing.Size(326, 75);
            this.ResumeLayout(false);
            this.PerformLayout();
       
        }



        //重写布局方法
        protected override void OnLayout(LayoutEventArgs e)
        {
            base.OnLayout(e);

            // 获取子控件
            if (this.Controls.Count == 0) return;
            Control c = this.Controls[0];

            // 父窗口参数
            Padding p = this.Padding;
            int x = 0, y = 0;
            int w = this.Width, h = this.Height;
            w -= (p.Left + p.Right);
            x += p.Left;

            // 计算文本框的高度，使其显示在中间
            int h2 = c.PreferredSize.Height;
            if (h2 > h) h2 = h;
            y = (h - h2) / 2;

            c.Location = new Point(x, y);
            c.Size = new Size(w, h2);
        }

      public   MyTestTextBox()
        {
            InitializeComponent();
        }
    }
}
